package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.AuthService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Author: Administrator
 * CreateTime: 2024/10/10
 * Project: heima-leadnews
 */
@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class AuthServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements AuthService {
    /**
     * 分页查询
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult list(AuthDto authDto) {
        // 1.校验参数
        authDto.checkParam();

        // 2.查询
        IPage<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());
        if (authDto.getStatus() == null) {
            page = page(page);
        } else {
            LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
            page = page(page, lambdaQueryWrapper);
        }

        // 3.返回查询结果
        PageResponseResult pageResponseResult = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }


    /**
     * 审核失败
     *
     * @param authDto
     * @return
     */
    @SneakyThrows
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        // 调用公共方法处理重复逻辑
        return processAuth(authDto, (short) 2, authDto.getMsg());
    }

    /**
     * 审核成功
     *
     * @param authDto
     * @return
     */
    @SneakyThrows
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        // 调用公共方法处理重复逻辑
        return processAuth(authDto, (short) 9, null);
    }

    /**
     * 处理审核逻辑
     *
     * @param authDto
     * @param status
     * @return
     */
    @SneakyThrows
    private ResponseResult processAuth(AuthDto authDto, short status, String reason) {
        // 参数校验
        authDto.checkParam();

        ApUserRealname realname = getById(authDto.getId());
        if (realname == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        realname.setStatus(status);
        realname.setUpdatedTime(new Date());
        if (StringUtils.isNotBlank(reason)) realname.setReason(reason);
        updateById(realname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
